package com.tsd.common.service.impl;

import com.tsd.common.dao.CommonTableSettingMapper;
import com.tsd.common.entity.CommonTableSetting;
import com.tsd.common.entity.CommonTableSettingExt;
import com.tsd.common.service.CommonTableSettingService;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.vo.PageBean;
import com.tsd.service.BaseServiceImpl;
import com.tsd.system.entity.SysUser;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 描述：CommonTableSetting 服务实现层
 *
 * @author Hillpool
 * @date 2022/10/19 17:54:36
 */
@Service
public class CommonTableSettingServiceImpl extends BaseServiceImpl implements CommonTableSettingService {

    @Resource
    private CommonTableSettingMapper commonTableSettingMapper;

    @Override
    public CommonTableSetting getCommonTableSettingById(Long id) throws Exception {
        return commonTableSettingMapper.selectByPrimaryKey(id);
    }

    @Override
    public CommonTableSettingExt queryBySid(String sid) throws Exception {
        return commonTableSettingMapper.selectBySid(sid);
    }

    @Override
    public void deleteCommonTableSettings(List<String> list, SysUser opUser) throws Exception {
        for (String sid : list) {
            CommonTableSetting old = commonTableSettingMapper.selectBySid(sid);
            super.checkEmpty(old, "该记录不存在");
            super.autoInjectBaseData(old, opUser, TYPE_DELETE);
            commonTableSettingMapper.updateByPrimaryKeySelective(old);
        }
    }

    @Override
    public void saveCommonTableSetting(CommonTableSettingExt record, SysUser opUser) throws Exception {
        if (HlpUtils.isEmpty(record.getId())) {
            super.autoInjectBaseData(record, opUser, this.TYPE_CREATE);
            commonTableSettingMapper.insertSelective(record);
        } else {
            super.autoInjectBaseData(record, opUser, this.TYPE_UPDATE);
            commonTableSettingMapper.updateByPrimaryKeySelective(record);
        }
    }

    @Override
    public List<CommonTableSettingExt> findCommonTableSettings(Map<String, Object> params, PageBean page) throws Exception {
        return commonTableSettingMapper.selectByPage(params, page);
    }

    @Override
    public List<CommonTableSettingExt> findCommonTableSettings(Map<String, Object> params) throws Exception {
        return commonTableSettingMapper.selectByParams(params);
    }

    @Override
    public CommonTableSettingExt queryCommonTableSettingByRouter(CommonTableSettingExt params) throws Exception {
        List<CommonTableSettingExt> list = commonTableSettingMapper.selectByEntity(params);
        if (!HlpUtils.isEmptyList(list)) {
            return list.get(0);
        }
        return null;
    }
}